<template>
  <div class="pre-order">
    <h2 style="text-align: center">笔记本电脑预购情况</h2>
    <br />
    <el-table :data="preOrderData">
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="price" label="价格"></el-table-column>
      <el-table-column prop="quantity" label="数量"></el-table-column>
      <el-table-column prop="deliveryDate" label="尾款日期"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="editProduct(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            @click="deleteProduct(scope.$index)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :visible.sync="dialogVisible"
      title="编辑产品信息"
      :append-to-body="true"
    >
      <el-form :model="editProductForm" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="editProductForm.name"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="editProductForm.price"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input v-model="editProductForm.quantity"></el-input>
        </el-form-item>
        <el-form-item label="尾款日期">
          <el-date-picker
            type="date"
            v-model="editProductForm.deliveryDate"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveProduct">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
export default {
  name: "Test4",
  data() {
    return {
      preOrderData: [],
   
      dialogVisible: false,
      editProductForm: {
        name: "",
        price: "",
        quantity: "",
        deliveryDate: "",
      },
      editProductIndex: -1,
        chartData: {}, // 添加此项并初始化为空对象
    };
  },

  mounted() {
    this.generateData();
  },

  methods: {
    generateData() {
  const laptops = [
    { name: "戴尔G15", price: 7800, deliveryDate: "2023-05-01" },
    { name: "天选3", price: 8400, deliveryDate: "2023-05-10" },
    { name: "外星人", price: 15000, deliveryDate: "2023-05-20" },
    { name: "华硕", price: 8000, deliveryDate: "2023-06-01" },
    { name: "绿标", price: 5400, deliveryDate: "2023-06-10" },
  ];
  const data = [];
  for (let i = 0; i < laptops.length; i++) {
    const quantity = Math.floor(Math.random() * 10) + 1;
    data.push({
      name: laptops[i].name,
      value: quantity,
      price: laptops[i].price,
      deliveryDate: laptops[i].deliveryDate,
    });
    this.preOrderData.push({
      ...laptops[i],
      quantity,
    });
  }
  this.chartData = {
    legend: {
      data: ['数量'],
    },
    xAxis: {
      data: data.map(item => item.name),
    },
    yAxis: {},
    series: [{
      name: '数量',
      type: 'bar',
      data: data.map(item => item.value),
    }],
  };
},

    editProduct(product) {
      this.editProductForm = { ...product };
      this.editProductIndex = this.preOrderData.indexOf(product);
      this.dialogVisible = true;
    },

    saveProduct() {
      if (this.editProductIndex === -1) {
        return;
      }
      this.preOrderData.splice(this.editProductIndex, 1, this.editProductForm);
      this.dialogVisible = false;
    },

    deleteProduct(index) {
      this.preOrderData.splice(index, 1);
    },
  },
};
</script>
  
  <style lang="scss" scoped>
</style>